//
//  FMRadio.swift
//  FMRadio
//
//  Created by 范东 on 2022/7/26.
//

import UIKit

let SDKAPPID = 1400712026

extension String{
    static let serverURL = "http://fmradio.app.xiaobingkj.com/"
    
    static let tokenKey = "tokenKey"
    static let user_idKey = "user_idKey"
    static let userSigKey = "userSigKey"
    
    static let FullGroupId = "@TGS#aIAVJ6TIN"
    
}

class FMRadio: NSObject {
    
    static let shared = FMRadio()
    var tabC: TabBarController?
    
    @objc public static func getCurrentWindow() -> UIWindow
    {
        if let window :UIWindow = (UIApplication.shared.delegate?.window)! {
            return window
        }else{
            let array:Set = UIApplication.shared.connectedScenes
            if array.count > 0 {
                let windowScene :UIWindowScene = array.first as! UIWindowScene
                if let mainWindow:UIWindow = windowScene.value(forKeyPath:"delegate.window") as? UIWindow{
                    return mainWindow
                }else{
                    if UIApplication.shared.windows.count > 0 {
                        return UIApplication.shared.windows.first!
                    }
                }
            }else{
                if UIApplication.shared.windows.count > 0 {
                    return UIApplication.shared.windows.first!
                }
            }
        }
        return UIWindow()
    }
    
    public static func getUserId() -> Int{
        let user_id = UserDefaults.standard.integer(forKey: .user_idKey)
        return user_id
    }
    
    public static func getToken() -> String{
        let token = UserDefaults.standard.string(forKey: .tokenKey)
        return token ?? ""
    }
    
    public static func getUserSig() -> String{
        let userSig = UserDefaults.standard.string(forKey: .userSigKey)
        return userSig ?? ""
    }
    
    public static func saveUser(dict: [String: Any]){
        let user_id = dict["user_id"] as! Int
        let token = dict["token"] as! String
        let userSig = dict["userSig"] as! String
        UserDefaults.standard.setValue(user_id, forKey: .user_idKey)
        UserDefaults.standard.setValue(token, forKey: .tokenKey)
        UserDefaults.standard.setValue(userSig, forKey: .userSigKey)
        UserDefaults.standard.synchronize()
        debugPrint("用户信息保存成功")
    }

    
    public static func logout(){
        TUILogin.logout {
            
        } fail: { code, msg in
            debugPrint("退出登录失败 code:\(code) msg"+(msg ?? ""))
        }
    }
}

extension UIImage{
    static let iconPlaceHolderImage = UIImage(named: "")
}
